home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / lib / mathlib / psort / example / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  4.0 KB  |  169 lines

  1. /* *****************************************************************************
  2. *
  3. * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  7. * the contents of this file may not be disclosed to third parties, copied or
  8. * duplicated in any form, in whole or in part, without the prior written
  9. * permission of Silicon Graphics, Inc.
  10. *
  11. * RESTRICTED RIGHTS LEGEND:
  12. * Use, duplication or disclosure by the Government is subject to restrictions
  13. * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  14. * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  15. * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  16. * rights reserved under the Copyright Laws of the United States.
  17. *
  18. ***************************************************************************** */
  19. #include <stdio.h>
  20. #include <device.h>
  21. #include "event.h"
  22.  
  23.  
  24. typedef struct event_s
  25. {
  26.     int        window, device, state;
  27.     void (*func)(void *, int);
  28.     char *arg;
  29.     struct event_s    *next;
  30. } event_t, *event_p;
  31.  
  32. typedef struct update_s
  33. {
  34.     int *flag;
  35.     void (*ufunc)(void *);
  36.     char *arg;
  37.     struct update_s *next;
  38. } update_t, *update_p;
  39.  
  40.  
  41. static event_p        event_list;
  42. static update_p    update_list;
  43.  
  44. /*
  45.  * This routine adds an event to be checked for to the event queue.
  46.  * window should be the wid of the window to respond in, or ANY if
  47.  * this event applies to all windows.  device is the device, and state
  48.  * is the device's value (e.g.  ANY, UP, DOWN, the window id (for
  49.  * REDRAW), etc).  Func is the function that will be called, with
  50.  * arguments 'arg' and the device's value.
  51.  * 
  52.  * NOTE:  the device must be queued for it to be found by the event()
  53.  * routine-- add_event DOES NOT qdevice(device).
  54.  */
  55. void
  56. add_event(window, device, state, func, arg)
  57. int    window, device, state;
  58. void (*func)(void *, int);
  59. char *arg;
  60. {
  61.     event_p new_guy;
  62.  
  63.     new_guy = (event_p)malloc(sizeof(event_t));
  64.     new_guy->window = window;
  65.     new_guy->device = device;
  66.     new_guy->state  = state;
  67.     new_guy->func   = func;
  68.     new_guy->arg    = arg;
  69.     new_guy->next   = event_list;
  70.     event_list = new_guy;
  71. }
  72.  
  73. /*
  74.  *    Specify a function to be called if there is nothing in the queue
  75.  * and (*flag) is non-zero.  If no update function is active, or
  76.  * (*flag) is 0, then event() will block on a qread.  If there is an
  77.  * active update function, event() will continuously call the update
  78.  * function, hogging the cpu.
  79.  */
  80. void
  81. add_update(flag, ufunc, arg)
  82. int    *flag;
  83. void (*ufunc)(void *);
  84. char *arg;
  85. {
  86.     update_p    new_guy;
  87.  
  88.     new_guy = (update_p)malloc(sizeof(update_t));
  89.     new_guy->flag = flag;
  90.     new_guy->ufunc = ufunc;
  91.     new_guy->arg  = arg;
  92.     new_guy->next = update_list;
  93.     update_list = new_guy;
  94. }
  95.  
  96. /*
  97.  * The main Event.  Call this repeatedly to automagically handle
  98.  * reading the queue, and calling your functions to handle what
  99.  * appears there.
  100.  */
  101. void
  102. event()
  103. {
  104.     void find_event(void), event_inputchange(void);
  105.     int find_update(void);
  106.     static int initialized = 0;
  107.     
  108.     if (initialized == 0)
  109.     {
  110.         add_event(ANY, INPUTCHANGE, ANY, event_inputchange, NULL);
  111.         qdevice(INPUTCHANGE);
  112.         initialized = 1;
  113.     }
  114.  
  115.     /* Something in the queue?  Handle it */
  116.     while (qtest())
  117.         find_event();
  118.     /*
  119.      * Or, if there's no update function, wait for something to appear
  120.      */
  121.     if (find_update() == 0)
  122.         find_event();
  123. }
  124.  
  125. int
  126. find_update()
  127. {
  128.     update_p    scan;
  129.     int        updated = 0;
  130.  
  131.     for (scan = update_list; scan && updated == 0; scan = scan->next)
  132.     {
  133.         if (*scan->flag)
  134.         {
  135.             (*scan->ufunc)(scan->arg);
  136.             updated = 1;
  137.         }
  138.     }
  139.  
  140.     return(updated);
  141. }
  142.  
  143. int context, state, device;
  144.  
  145. void
  146. event_inputchange()
  147. {
  148.     context = winget();
  149. }
  150.  
  151. void
  152. find_event()
  153. {
  154.     event_p scan;
  155.     short    s;
  156.  
  157.     device = qread(&s);
  158.     state = s;
  159.     for (scan = event_list; scan; scan = scan->next)
  160.     {
  161.         if ( ((scan->window == ANY) || (context == scan->window))
  162.             && ((scan->device == ANY) ||(device == scan->device))
  163.             && ((scan->state == ANY) || (state == scan->state)))
  164.         {
  165.             (*scan->func)(scan->arg, state);
  166.         }
  167.     }
  168. }
  169.